package main

import (
	"os/exec"
	"strings"
)

// HasGolang 检查是否有安装golang
func HasGolang() bool {
	cmd := exec.Command(goExec, "version")
	output, err := cmd.CombinedOutput()
	if err != nil {
		return false
	}
	if strings.Contains(string(output), "go version") {
		return true
	}
	return false
}

// FormatFile 格式化go文件
var (
	formatFlag  = false
	formatHasGo = false
)

func FormatFile(file string) {
	if !formatFlag {
		formatHasGo = HasGolang()
		formatFlag = true
	}

	if !formatHasGo {
		return
	}

	//log.Println(goFmtExec, file)
	cmd := exec.Command(goFmtExec, "-w", file)
	_, _ = cmd.CombinedOutput()
}
